import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import "rxjs/add/operator/map"; 
import { UtilsProvider } from '../utils/utils';
/*
  Generated class for the HttpProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
const httpOptions =  {
  headers:new HttpHeaders( {
      "Accept":"application/json", 
      "Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"})
};
@Injectable()
export class HttpProvider {
  constructor(public http: HttpClient, private utils:UtilsProvider) {
    console.log('Hello HttpProvider Provider');
  }
  //====================================================
        // ********** 优化封装
  //====================================================
  /**
   * get请求 参数可以直接拼在url后面
   * @param url 
   * @param successCallback 
   * @param errorCallback 
   * 
   * 
   */
  GET(url:string, successCallback, errorCallback):any {
    this.http.get(url, httpOptions).subscribe(res =>  {
        // console.log("resut===="+JSON.stringify(res));
        if(res['code'] == 200) {
            successCallback && successCallback(res['data']);
        } else {
            this.utils.presentLoadingText(res['msg']);
        }
    }, err =>  {
        console.log('网络请求出错.....'+JSON.stringify(err));
        errorCallback && errorCallback(err);
    }); 
  }

  // HttpParams的构造方法里提供了两种方式去构造参数
  // 1.new HttpParams({fromString: "param1=1&param2=2"})
  // 2.new HttpParams({fromObject: {param1 : 1, param2 : 2}})
  encodeHttpParams(params: any): any {
    if (!params) return null;
    return new HttpParams({fromObject: params});
  }

  /**
   * post请求
   * @param url 
   * @param params 
   * @param successCallback 
   * @param errorCallback 
   * 
   * 
   */
  POST(url:string, params:any, successCallback, errorCallback):any {
      // let hotMoviesUrl:string = "http:/访问项目的ip/myapp/login";
      // params = '{"user": "测试1"}'
      this.http.post(url, httpOptions, params).subscribe(res =>  { 
          console.log("resut===="+JSON.stringify(res));
          if(res['code'] == 200) {
            successCallback && successCallback(res['data']);
          } else {
            this.utils.presentLoadingText(res['msg']);
          }
      }, err =>  {
        console.log('网络请求出错.....'+err)
        errorCallback && errorCallback(err);
      }); 
  }

  //====================================================
        // **********  原始封装
  //====================================================
   //get请求
   oldGet(url:string, successCallback, errorCallback):any {
    this.http.get(url, httpOptions).subscribe(res =>  {
        // console.log("res==="+res);
        successCallback && successCallback(res); 
    }, err =>  {
        errorCallback && errorCallback(err); 
    }); 
  }

  //post请求
  oldPost(url:string, params:any, successCallback, errorCallback):any {
      // let hotMoviesUrl:string = "http:/访问项目的ip/myapp/login"; 
      // params = '{"user": "测试1"}'
      this.http.post(url, params, httpOptions).subscribe(res =>  {
          successCallback && successCallback(res); 
      }, err =>  {
          errorCallback && errorCallback(err); 
      }); 
  }

}
